﻿using System;
using System.Windows.Forms;
using CuteSprite.Eme.Data.Settlement;
using CuteSprite.Eme.UI.Utils;
using HmFramework.Components;
using HmFramework.Components.Controls;
using HmFramework.Components.Editors;
using HmFramework.Data;
using HmFramework.UI.Utils;
using HmFramework.UI.Utils.Controls;

namespace CuteSprite.Eme.UI.Settlement
{
	public partial class FrmBsdPriceSolutionSingle : EmeEntityForm
	{
		#region -- 属性 --

		private Int32 _SettlementSolutionID;

		/// <summary>公布价方案ID</summary>
		public Int32 SettlementSolutionID
		{
			get { return _SettlementSolutionID; }
			set { _SettlementSolutionID = value; }
		}

		private Int32 _BillingSolutionID;

		/// <summary>计费方案ID</summary>
		public Int32 BillingSolutionID
		{
			get { return _BillingSolutionID; }
			set { _BillingSolutionID = value; }
		}

		private PriceSolution _CurrentPrice;

		/// <summary>当前计费价格方案实体对象</summary>
		public PriceSolution CurrentPrice
		{
			get
			{
				if (_CurrentPrice != null) { return _CurrentPrice; }
				_CurrentPrice = CurrentEntity as PriceSolution;
				return _CurrentPrice;
			}
			set { _CurrentPrice = value; }
		}

		private String _SettlementSolutionCode;

		/// <summary> 公布价方案代码 </summary>
		public String SettlementSolutionCode
		{
			get { return _SettlementSolutionCode; }
			set { _SettlementSolutionCode = value; }
		}
		#endregion

		#region -- 构造 --

		#region - 新增 -

		/// <summary>默认构造函数</summary>
		/// <param name="settlementSolutionID"></param>
		/// <param name="billingSolutionID"></param>
		/// <param name="entity"></param>
		public FrmBsdPriceSolutionSingle(Int32 settlementSolutionID, String settlementSolutionCode, Int32 billingSolutionID, IEntity entity)
			: base(entity, false)
		{
			SettlementSolutionID = settlementSolutionID;
			BillingSolutionID = billingSolutionID;
			SettlementSolutionCode = settlementSolutionCode;

			InitializeComponent();
			InitForm();
		}

		#endregion

		#region - 编辑 -

		/// <summary>构造函数</summary>
		/// <param name="settlementSolutionID"></param>
		/// <param name="entity">实体对象</param>
		public FrmBsdPriceSolutionSingle(Int32 settlementSolutionID, String settlementSolutionCode, IEntity entity)
			: base(entity, false)
		{
			SettlementSolutionID = settlementSolutionID;
			SettlementSolutionCode = settlementSolutionCode;

			InitializeComponent();
			InitForm();
		}

		/// <summary>构造函数</summary>
		/// <param name="settlementSolutionID"></param>
		/// <param name="entity">实体对象</param>
		/// <param name="readOnly">是否只读，指定编辑还是浏览实体信息</param>
		public FrmBsdPriceSolutionSingle(Int32 settlementSolutionID, String settlementSolutionCode, IEntity entity, Boolean readOnly)
			: base(entity, readOnly)
		{
			SettlementSolutionID = settlementSolutionID;
			SettlementSolutionCode = settlementSolutionCode;

			InitializeComponent();
			InitForm();
		}

		#endregion

		#endregion

		#region -- 窗体事件 --

		#region - method InitComponent -

		///// <summary>控件初始化</summary>
		protected override void InitComponent()
		{
			base.InitComponent();

			#region 控件事件

			if (frmIsMultiPrice == null) { frmIsMultiPrice = FindControl("frmIsMultiPrice") as CheckBoxX; }
			if (frmPrimaryPrice == null) { frmPrimaryPrice = FindControl("frmPrimaryPrice") as NumberInput; }
			if (frmExtraPrice == null) { frmExtraPrice = FindControl("frmExtraPrice") as NumberInput; }

			#region 初始化区分首重与续重

			if (frmIsMultiPrice != null)
			{
				frmIsMultiPrice.CheckedChanged += new EventHandler(frmIsMultiPrice_CheckedChanged);
			}

			#endregion

			#endregion

			this.OnValid += new System.EventHandler<CuteSprite.Eme.UI.Utils.EntityFormEventArgs>(this.FrmBsdPriceSolutionSingle_OnValid);
			this.OnSaving += new System.EventHandler<CuteSprite.Eme.UI.Utils.EntityFormEventArgs>(this.FrmBsdPriceSolutionSingle_OnSaving);
		}

		#endregion

		#region - method FormOnPreLoad -

		protected override void FormOnPreLoad()
		{
			base.FormOnPreLoad();

			if (IsAddNew)
			{
				CurrentPrice.BillingSolutionID = BillingSolutionID;
			}
		}

		#endregion

		#region - method FormOnLoad -

		public override void FormOnLoad()
		{
			base.FormOnLoad();
			if (!IsAddNew)
			{
				if (CurrentPrice.WeightBasedBilling == 1M)
				{
					rbWeightBasedBilling2.Checked = true;
				}
				else
				{
					rbWeightBasedBilling1.Checked = true;
				}
				//rbWeightBasedBilling1.Checked
				if (frmIsMultiPrice == null) { frmIsMultiPrice = FindControl("frmIsMultiPrice") as CheckBoxX; }
				//if (frmIsDiscountType != null || frmIsMultiPrice != null)
				//{
				//  if (frmIsDiscountType.Checked)
				//  {
				//    frmPrimaryPrice.Value = 0;
				//    frmExtraPrice.Value = 0;
				//  }
				//  else
				//  {
				//    frmPrimaryPrice.Value = CurrentPrice.PrimaryPrice;
				//    frmExtraPrice.Value = CurrentPrice.ExtraPrice;
				//  }
				//}
			}

			#region 是否为默认公布价方案

			//if (frmIsDiscountType != null)
			//{
			//  try
			//  {
			//    lock (ServerManagement.LockSynchronizer)
			//    {
			//      var settlementsolution = ServerManagement.LoadEntityWithName(SettlementSolution.Meta.Factory, "EmeSettlement", "SettlementSolution", SettlementSolution.__.ID, SettlementSolutionID) as SettlementSolution;
			//      if (settlementsolution != null)
			//      {
			//        if (settlementsolution.Code.Equals("DS"))
			//        {
			//          frmIsDiscountType.Checked = false;
			//          frmIsDiscountType.Enabled = false;
			//        }
			//        else
			//        {
			//          frmIsDiscountType.Enabled = true;
			//        }
			//      }
			//    }
			//  }
			//  catch (Exception ex)
			//  {
			//    //HmTrace.WriteWarn(ex.ToString());
			//    DialogHelper.ShowWarning(null, ex.Message);
			//  }
			//}

			#endregion
		}

		#endregion

		#endregion

		#region -- 实体保存 --

		private void FrmBsdPriceSolutionSingle_OnSaving(object sender, EntityFormEventArgs e)
		{
			#region 区分首重与续重,折扣模式

			CurrentPrice.WeightBasedBilling = rbWeightBasedBilling2.Checked ? 1M : 0.5M;

			#endregion
		}

		#endregion

		#region -- 实体验证 --

		private void FrmBsdPriceSolutionSingle_OnValid(object sender, EntityFormEventArgs e)
		{

			if (CurrentPrice.PrimaryPrice <= 0M)
			{
				MessageBoxEx.Show(CurrentPrice.IsMultiPrice ? "首重价格必须大于0！" : "计费价格必须大于0！", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
				e.Cancel = true;
				return;
			}
			if (CurrentPrice.IsMultiPrice && CurrentPrice.ExtraPrice <= 0M)
			{
				MessageBoxEx.Show("续重价格必须大于0！", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
				e.Cancel = true;
				return;
			}
		}

		#endregion

		private void frmIsMultiPrice_CheckedChanged(object sender, EventArgs e)
		{
			var isMultiPrice = frmIsMultiPrice.Checked;
			ExtraPriceLable.Visible = isMultiPrice;
			frmExtraPrice.Visible = isMultiPrice;

			if (isMultiPrice)
			{
				frmExtraPrice.Enabled = true;
				PrimaryPriceLabel.Text = "首重计费：";
				frmExtraPrice.TabStop = true;
			}
			else
			{
				frmExtraPrice.Enabled = false;
				PrimaryPriceLabel.Text = "计费价格：";
				frmExtraPrice.TabStop = false;
			}
		}
	}
}